<?php

namespace app\admin\controller;
use app\common\controller\Backend;
use think\DB;
use think\Request;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Top extends Backend
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $array=session('admin');
        $where['id']=$array['id'];
        $uesr=DB::name('user')->where($where)->find();
        $this->user=$uesr;
    }
    function index(){
        //会员资料
        $mobile=$this->request->param('mobile');
        $where=array();
        $user=$this->user;
        if($user['id'] !=1)
        {
            $where['t.store_id']=$user['store_id'];
        }
        $list = Db::name('top_config')->alias('t')->join('store s','s.id = t.store_id')->where($where)->order('t.id desc')->field('t.*,s.store_name')->paginate(10);
        if($list->toArray())
        {
            foreach($list as $k=>$v)
            {
                $data=array();
                $data=$v;
                if($v['state']==1)
                {
                    $data['state_name']='启用';
                }else{
                    $data['state_name']='禁用';
                }
                $list->offsetSet($k,$data);
            }
        }
        //$list->appends(array('mobile'=>$mobile));
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign('list',$list);
        return $this->fetch('top/index');
    }
    function delete(){
        $param = $this->request->param();
        $param=$param['id'];
        DB::name('member')->where('id',$param)->delete();
        $this->success('删除成功');
    }
    function consume(){
        //会员消费记录查询
        return $this->fetch();
    }
    function detail(){
        $param = $this->request->param();
        $paramt=$param['id'];
        $top=DB::name('top')->alias('t')->join('store s','s.id = t.store_id')->where('t.id',$paramt)->field('t.*,s.store_name')->find();
        $this->assign('top',$top);
        return $this->fetch('top/dateil');
    }
    function detailpost()
    {
        $id=$this->request->param('id');
        $where['id']=$id;
        $date['top_money']=$_POST['top_money'];
        $date['give_money']=$_POST['give_money'];
        $date['state']=$_POST['state'];
        $date['time']=time();
        $state=DB::name('top')->where($where)->update($date);
        if($state)
        {
            return $this->success('修改成功!','top/top');
        }else{
            return $this->success('修改失败!');
        }
    }
    /*
     *
     * 判断是否是admin
     * 选择店铺
     * */
    function add()
    {
        $user=$this->user;
        $where['state']=1;
        if($user['id']!=1)
        {
            $where['id']=$user['store_id'];
        }
        $store=DB::name('store')->where($where)->select();
        $this->assign('store',$store);
        return $this->fetch();
    }
    /*
     *
     * */
    function addpost()
    {
        $date['top_money']=$_POST['top_money'];
        $date['give_money']=$_POST['give_money'];
        $date['time']=time();
        $date['store_id']=$_POST['store'];

        $state=DB::name('top')->insert($date);
        if($state)
        {
            return $this->success('添加成功!','top/top');
        }else{
            return $this->success('添加失败!');
        }

    }
}
